package cn.ibizlab.assetmanagement.assetapi.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.assetmanagement.assetapi.dto.*;
import cn.ibizlab.assetmanagement.assetapi.mapping.*;
import cn.ibizlab.assetmanagement.core.plan.domain.EAMPlan;
import cn.ibizlab.assetmanagement.core.plan.service.IEAMPlanService;
import cn.ibizlab.assetmanagement.core.plan.filter.EAMPlanSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"维护计划" })
@RestController("AssetApi-eamplan")
@RequestMapping("")
public class EAMPlanResource {

    @Autowired
    public IEAMPlanService eamplanService;

    @Autowired
    @Lazy
    public EAMPlanMapping eamplanMapping;

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandto),'iBizAssetManagement-EAMPlan-Create')")
    @ApiOperation(value = "新建维护计划", tags = {"维护计划" },  notes = "新建维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplans")
    @Transactional
    public ResponseEntity<EAMPlanDTO> create(@RequestBody EAMPlanDTO eamplandto) {
        EAMPlan domain = eamplanMapping.toDomain(eamplandto);
		eamplanService.create(domain);
        EAMPlanDTO dto = eamplanMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandtos),'iBizAssetManagement-EAMPlan-Create')")
    @ApiOperation(value = "批量新建维护计划", tags = {"维护计划" },  notes = "批量新建维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplans/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMPlanDTO> eamplandtos) {
        eamplanService.createBatch(eamplanMapping.toDomain(eamplandtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamplan" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamplanService.get(#eamplan_id),'iBizAssetManagement-EAMPlan-Update')")
    @ApiOperation(value = "更新维护计划", tags = {"维护计划" },  notes = "更新维护计划")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamplans/{eamplan_id}")
    @Transactional
    public ResponseEntity<EAMPlanDTO> update(@PathVariable("eamplan_id") String eamplan_id, @RequestBody EAMPlanDTO eamplandto) {
		EAMPlan domain  = eamplanMapping.toDomain(eamplandto);
        domain .setEamplanid(eamplan_id);
		eamplanService.update(domain );
		EAMPlanDTO dto = eamplanMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplanService.getEamplanByEntities(this.eamplanMapping.toDomain(#eamplandtos)),'iBizAssetManagement-EAMPlan-Update')")
    @ApiOperation(value = "批量更新维护计划", tags = {"维护计划" },  notes = "批量更新维护计划")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamplans/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMPlanDTO> eamplandtos) {
        eamplanService.updateBatch(eamplanMapping.toDomain(eamplandtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamplanService.get(#eamplan_id),'iBizAssetManagement-EAMPlan-Remove')")
    @ApiOperation(value = "删除维护计划", tags = {"维护计划" },  notes = "删除维护计划")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamplans/{eamplan_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamplan_id") String eamplan_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamplanService.remove(eamplan_id));
    }

    @PreAuthorize("hasPermission(this.eamplanService.getEamplanByIds(#ids),'iBizAssetManagement-EAMPlan-Remove')")
    @ApiOperation(value = "批量删除维护计划", tags = {"维护计划" },  notes = "批量删除维护计划")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamplans/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamplanService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamplanMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMPlan-Get')")
    @ApiOperation(value = "获取维护计划", tags = {"维护计划" },  notes = "获取维护计划")
	@RequestMapping(method = RequestMethod.GET, value = "/eamplans/{eamplan_id}")
    public ResponseEntity<EAMPlanDTO> get(@PathVariable("eamplan_id") String eamplan_id) {
        EAMPlan domain = eamplanService.get(eamplan_id);
        EAMPlanDTO dto = eamplanMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取维护计划草稿", tags = {"维护计划" },  notes = "获取维护计划草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamplans/getdraft")
    public ResponseEntity<EAMPlanDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamplanMapping.toDto(eamplanService.getDraft(new EAMPlan())));
    }

    @ApiOperation(value = "检查维护计划", tags = {"维护计划" },  notes = "检查维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplans/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMPlanDTO eamplandto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamplanService.checkKey(eamplanMapping.toDomain(eamplandto)));
    }

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandto),'iBizAssetManagement-EAMPlan-Save')")
    @ApiOperation(value = "保存维护计划", tags = {"维护计划" },  notes = "保存维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplans/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMPlanDTO eamplandto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamplanService.save(eamplanMapping.toDomain(eamplandto)));
    }

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandtos),'iBizAssetManagement-EAMPlan-Save')")
    @ApiOperation(value = "批量保存维护计划", tags = {"维护计划" },  notes = "批量保存维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplans/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMPlanDTO> eamplandtos) {
        eamplanService.saveBatch(eamplanMapping.toDomain(eamplandtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlan-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlan-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"维护计划" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamplans/fetchdefault")
	public ResponseEntity<List<EAMPlanDTO>> fetchDefault(EAMPlanSearchContext context) {
        Page<EAMPlan> domains = eamplanService.searchDefault(context) ;
        List<EAMPlanDTO> list = eamplanMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlan-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlan-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"维护计划" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamplans/searchdefault")
	public ResponseEntity<Page<EAMPlanDTO>> searchDefault(@RequestBody EAMPlanSearchContext context) {
        Page<EAMPlan> domains = eamplanService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamplanMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandto),'iBizAssetManagement-EAMPlan-Create')")
    @ApiOperation(value = "根据资产建立维护计划", tags = {"维护计划" },  notes = "根据资产建立维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets/{eamasset_id}/eamplans")
    @Transactional
    public ResponseEntity<EAMPlanDTO> createByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMPlanDTO eamplandto) {
        EAMPlan domain = eamplanMapping.toDomain(eamplandto);
        domain.setAssetid(eamasset_id);
		eamplanService.create(domain);
        EAMPlanDTO dto = eamplanMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandtos),'iBizAssetManagement-EAMPlan-Create')")
    @ApiOperation(value = "根据资产批量建立维护计划", tags = {"维护计划" },  notes = "根据资产批量建立维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets/{eamasset_id}/eamplans/batch")
    public ResponseEntity<Boolean> createBatchByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @RequestBody List<EAMPlanDTO> eamplandtos) {
        List<EAMPlan> domainlist=eamplanMapping.toDomain(eamplandtos);
        for(EAMPlan domain:domainlist){
            domain.setAssetid(eamasset_id);
        }
        eamplanService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamplan" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamplanService.get(#eamplan_id),'iBizAssetManagement-EAMPlan-Update')")
    @ApiOperation(value = "根据资产更新维护计划", tags = {"维护计划" },  notes = "根据资产更新维护计划")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassets/{eamasset_id}/eamplans/{eamplan_id}")
    @Transactional
    public ResponseEntity<EAMPlanDTO> updateByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @PathVariable("eamplan_id") String eamplan_id, @RequestBody EAMPlanDTO eamplandto) {
        EAMPlan domain = eamplanMapping.toDomain(eamplandto);
        domain.setAssetid(eamasset_id);
        domain.setEamplanid(eamplan_id);
		eamplanService.update(domain);
        EAMPlanDTO dto = eamplanMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplanService.getEamplanByEntities(this.eamplanMapping.toDomain(#eamplandtos)),'iBizAssetManagement-EAMPlan-Update')")
    @ApiOperation(value = "根据资产批量更新维护计划", tags = {"维护计划" },  notes = "根据资产批量更新维护计划")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassets/{eamasset_id}/eamplans/batch")
    public ResponseEntity<Boolean> updateBatchByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @RequestBody List<EAMPlanDTO> eamplandtos) {
        List<EAMPlan> domainlist=eamplanMapping.toDomain(eamplandtos);
        for(EAMPlan domain:domainlist){
            domain.setAssetid(eamasset_id);
        }
        eamplanService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamplanService.get(#eamplan_id),'iBizAssetManagement-EAMPlan-Remove')")
    @ApiOperation(value = "根据资产删除维护计划", tags = {"维护计划" },  notes = "根据资产删除维护计划")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamassets/{eamasset_id}/eamplans/{eamplan_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @PathVariable("eamplan_id") String eamplan_id) {
		return ResponseEntity.status(HttpStatus.OK).body(eamplanService.remove(eamplan_id));
    }

    @PreAuthorize("hasPermission(this.eamplanService.getEamplanByIds(#ids),'iBizAssetManagement-EAMPlan-Remove')")
    @ApiOperation(value = "根据资产批量删除维护计划", tags = {"维护计划" },  notes = "根据资产批量删除维护计划")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamassets/{eamasset_id}/eamplans/batch")
    public ResponseEntity<Boolean> removeBatchByEAMAsset(@RequestBody List<String> ids) {
        eamplanService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamplanMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMPlan-Get')")
    @ApiOperation(value = "根据资产获取维护计划", tags = {"维护计划" },  notes = "根据资产获取维护计划")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassets/{eamasset_id}/eamplans/{eamplan_id}")
    public ResponseEntity<EAMPlanDTO> getByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @PathVariable("eamplan_id") String eamplan_id) {
        EAMPlan domain = eamplanService.get(eamplan_id);
        EAMPlanDTO dto = eamplanMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据资产获取维护计划草稿", tags = {"维护计划" },  notes = "根据资产获取维护计划草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/eamassets/{eamasset_id}/eamplans/getdraft")
    public ResponseEntity<EAMPlanDTO> getDraftByEAMAsset(@PathVariable("eamasset_id") String eamasset_id) {
        EAMPlan domain = new EAMPlan();
        domain.setAssetid(eamasset_id);
        return ResponseEntity.status(HttpStatus.OK).body(eamplanMapping.toDto(eamplanService.getDraft(domain)));
    }

    @ApiOperation(value = "根据资产检查维护计划", tags = {"维护计划" },  notes = "根据资产检查维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets/{eamasset_id}/eamplans/checkkey")
    public ResponseEntity<Boolean> checkKeyByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMPlanDTO eamplandto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamplanService.checkKey(eamplanMapping.toDomain(eamplandto)));
    }

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandto),'iBizAssetManagement-EAMPlan-Save')")
    @ApiOperation(value = "根据资产保存维护计划", tags = {"维护计划" },  notes = "根据资产保存维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets/{eamasset_id}/eamplans/save")
    public ResponseEntity<Boolean> saveByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMPlanDTO eamplandto) {
        EAMPlan domain = eamplanMapping.toDomain(eamplandto);
        domain.setAssetid(eamasset_id);
        return ResponseEntity.status(HttpStatus.OK).body(eamplanService.save(domain));
    }

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandtos),'iBizAssetManagement-EAMPlan-Save')")
    @ApiOperation(value = "根据资产批量保存维护计划", tags = {"维护计划" },  notes = "根据资产批量保存维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets/{eamasset_id}/eamplans/savebatch")
    public ResponseEntity<Boolean> saveBatchByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @RequestBody List<EAMPlanDTO> eamplandtos) {
        List<EAMPlan> domainlist=eamplanMapping.toDomain(eamplandtos);
        for(EAMPlan domain:domainlist){
             domain.setAssetid(eamasset_id);
        }
        eamplanService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlan-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlan-Get')")
	@ApiOperation(value = "根据资产获取DEFAULT", tags = {"维护计划" } ,notes = "根据资产获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamassets/{eamasset_id}/eamplans/fetchdefault")
	public ResponseEntity<List<EAMPlanDTO>> fetchEAMPlanDefaultByEAMAsset(@PathVariable("eamasset_id") String eamasset_id,EAMPlanSearchContext context) {
        context.setN_assetid_eq(eamasset_id);
        Page<EAMPlan> domains = eamplanService.searchDefault(context) ;
        List<EAMPlanDTO> list = eamplanMapping.toDto(domains.getContent());
	    return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlan-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlan-Get')")
	@ApiOperation(value = "根据资产查询DEFAULT", tags = {"维护计划" } ,notes = "根据资产查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamassets/{eamasset_id}/eamplans/searchdefault")
	public ResponseEntity<Page<EAMPlanDTO>> searchEAMPlanDefaultByEAMAsset(@PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMPlanSearchContext context) {
        context.setN_assetid_eq(eamasset_id);
        Page<EAMPlan> domains = eamplanService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamplanMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandto),'iBizAssetManagement-EAMPlan-Create')")
    @ApiOperation(value = "根据功能位置资产建立维护计划", tags = {"维护计划" },  notes = "根据功能位置资产建立维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans")
    @Transactional
    public ResponseEntity<EAMPlanDTO> createByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMPlanDTO eamplandto) {
        EAMPlan domain = eamplanMapping.toDomain(eamplandto);
        domain.setAssetid(eamasset_id);
		eamplanService.create(domain);
        EAMPlanDTO dto = eamplanMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandtos),'iBizAssetManagement-EAMPlan-Create')")
    @ApiOperation(value = "根据功能位置资产批量建立维护计划", tags = {"维护计划" },  notes = "根据功能位置资产批量建立维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/batch")
    public ResponseEntity<Boolean> createBatchByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @RequestBody List<EAMPlanDTO> eamplandtos) {
        List<EAMPlan> domainlist=eamplanMapping.toDomain(eamplandtos);
        for(EAMPlan domain:domainlist){
            domain.setAssetid(eamasset_id);
        }
        eamplanService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamplan" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamplanService.get(#eamplan_id),'iBizAssetManagement-EAMPlan-Update')")
    @ApiOperation(value = "根据功能位置资产更新维护计划", tags = {"维护计划" },  notes = "根据功能位置资产更新维护计划")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/{eamplan_id}")
    @Transactional
    public ResponseEntity<EAMPlanDTO> updateByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @PathVariable("eamplan_id") String eamplan_id, @RequestBody EAMPlanDTO eamplandto) {
        EAMPlan domain = eamplanMapping.toDomain(eamplandto);
        domain.setAssetid(eamasset_id);
        domain.setEamplanid(eamplan_id);
		eamplanService.update(domain);
        EAMPlanDTO dto = eamplanMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplanService.getEamplanByEntities(this.eamplanMapping.toDomain(#eamplandtos)),'iBizAssetManagement-EAMPlan-Update')")
    @ApiOperation(value = "根据功能位置资产批量更新维护计划", tags = {"维护计划" },  notes = "根据功能位置资产批量更新维护计划")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/batch")
    public ResponseEntity<Boolean> updateBatchByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @RequestBody List<EAMPlanDTO> eamplandtos) {
        List<EAMPlan> domainlist=eamplanMapping.toDomain(eamplandtos);
        for(EAMPlan domain:domainlist){
            domain.setAssetid(eamasset_id);
        }
        eamplanService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamplanService.get(#eamplan_id),'iBizAssetManagement-EAMPlan-Remove')")
    @ApiOperation(value = "根据功能位置资产删除维护计划", tags = {"维护计划" },  notes = "根据功能位置资产删除维护计划")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/{eamplan_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @PathVariable("eamplan_id") String eamplan_id) {
		return ResponseEntity.status(HttpStatus.OK).body(eamplanService.remove(eamplan_id));
    }

    @PreAuthorize("hasPermission(this.eamplanService.getEamplanByIds(#ids),'iBizAssetManagement-EAMPlan-Remove')")
    @ApiOperation(value = "根据功能位置资产批量删除维护计划", tags = {"维护计划" },  notes = "根据功能位置资产批量删除维护计划")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/batch")
    public ResponseEntity<Boolean> removeBatchByEAMLocationEAMAsset(@RequestBody List<String> ids) {
        eamplanService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamplanMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMPlan-Get')")
    @ApiOperation(value = "根据功能位置资产获取维护计划", tags = {"维护计划" },  notes = "根据功能位置资产获取维护计划")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/{eamplan_id}")
    public ResponseEntity<EAMPlanDTO> getByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @PathVariable("eamplan_id") String eamplan_id) {
        EAMPlan domain = eamplanService.get(eamplan_id);
        EAMPlanDTO dto = eamplanMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据功能位置资产获取维护计划草稿", tags = {"维护计划" },  notes = "根据功能位置资产获取维护计划草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/getdraft")
    public ResponseEntity<EAMPlanDTO> getDraftByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id) {
        EAMPlan domain = new EAMPlan();
        domain.setAssetid(eamasset_id);
        return ResponseEntity.status(HttpStatus.OK).body(eamplanMapping.toDto(eamplanService.getDraft(domain)));
    }

    @ApiOperation(value = "根据功能位置资产检查维护计划", tags = {"维护计划" },  notes = "根据功能位置资产检查维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/checkkey")
    public ResponseEntity<Boolean> checkKeyByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMPlanDTO eamplandto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamplanService.checkKey(eamplanMapping.toDomain(eamplandto)));
    }

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandto),'iBizAssetManagement-EAMPlan-Save')")
    @ApiOperation(value = "根据功能位置资产保存维护计划", tags = {"维护计划" },  notes = "根据功能位置资产保存维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/save")
    public ResponseEntity<Boolean> saveByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMPlanDTO eamplandto) {
        EAMPlan domain = eamplanMapping.toDomain(eamplandto);
        domain.setAssetid(eamasset_id);
        return ResponseEntity.status(HttpStatus.OK).body(eamplanService.save(domain));
    }

    @PreAuthorize("hasPermission(this.eamplanMapping.toDomain(#eamplandtos),'iBizAssetManagement-EAMPlan-Save')")
    @ApiOperation(value = "根据功能位置资产批量保存维护计划", tags = {"维护计划" },  notes = "根据功能位置资产批量保存维护计划")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/savebatch")
    public ResponseEntity<Boolean> saveBatchByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @RequestBody List<EAMPlanDTO> eamplandtos) {
        List<EAMPlan> domainlist=eamplanMapping.toDomain(eamplandtos);
        for(EAMPlan domain:domainlist){
             domain.setAssetid(eamasset_id);
        }
        eamplanService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlan-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlan-Get')")
	@ApiOperation(value = "根据功能位置资产获取DEFAULT", tags = {"维护计划" } ,notes = "根据功能位置资产获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/fetchdefault")
	public ResponseEntity<List<EAMPlanDTO>> fetchEAMPlanDefaultByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id,EAMPlanSearchContext context) {
        context.setN_assetid_eq(eamasset_id);
        Page<EAMPlan> domains = eamplanService.searchDefault(context) ;
        List<EAMPlanDTO> list = eamplanMapping.toDto(domains.getContent());
	    return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlan-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlan-Get')")
	@ApiOperation(value = "根据功能位置资产查询DEFAULT", tags = {"维护计划" } ,notes = "根据功能位置资产查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}/eamplans/searchdefault")
	public ResponseEntity<Page<EAMPlanDTO>> searchEAMPlanDefaultByEAMLocationEAMAsset(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMPlanSearchContext context) {
        context.setN_assetid_eq(eamasset_id);
        Page<EAMPlan> domains = eamplanService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamplanMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

